package basicservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type AssetCategoryPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetCategoryPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetCategoryPageLogic {
	return &AssetCategoryPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetCategoryPage 分页查询资产分类表
func (l *AssetCategoryPageLogic) AssetCategoryPage(in *property_service.AssetCategoryPageReq) (*property_service.AssetCategoryPageRes, error) {
	// 定义一个资产分类表模型对象切片用于存储查询结果
	var data []*model.AssetCategory
	// 获取资产分类表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetCategory
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID))
	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		// 处理字符类型字段，进行模糊查询
		if in.Where.Name != "" {
			if in.Where.IsMc == 1 {
				do = do.Where(m.Name.Eq(in.Where.Name)) // 分类名称
			} else {
				do = do.Where(m.Name.Like("%" + in.Where.Name + "%")) // 分类名称
			}
		}
		if in.Where.CategoryCode != "" {
			do = do.Where(m.CategoryCode.Eq(in.Where.CategoryCode)) // 分类编号
		}
		if in.Where.PinyinCode != "" {
			do = do.Where(m.PinyinCode.Like("%" + in.Where.PinyinCode + "%")) // 拼音简码
		}
		if in.Where.ManufacturerName != "" {
			do = do.Where(m.ManufacturerName.Like("%" + in.Where.ManufacturerName + "%")) // 生产厂商名称
		}
		if in.Where.FinancialCategoryName != "" {
			do = do.Where(m.FinancialCategoryName.Like("%" + in.Where.FinancialCategoryName + "%")) // 财务分类名称
		}
		if in.Where.BrandName != "" {
			do = do.Where(m.BrandName.Like("%" + in.Where.BrandName + "%")) // 品牌名称
		}
		if in.Where.StatusName != "" {
			do = do.Where(m.StatusName.Like("%" + in.Where.StatusName + "%")) // 状态名称
		}
		if in.Where.Remark != "" {
			do = do.Where(m.Remark.Like("%" + in.Where.Remark + "%")) // 备注
		}
		if in.Where.Unit != "" {
			do = do.Where(m.Unit.Like("%" + in.Where.Unit + "%")) // 单位
		}
		if in.Where.IsComponent != "" {
			do = do.Where(m.IsComponent.Like("%" + in.Where.IsComponent + "%")) // 部件标志
		}
		if in.Where.ParentCode != "" {
			do = do.Where(m.ParentCode.Like("%" + in.Where.ParentCode + "%")) // 备注
		}
		if in.Where.NeedMaintenance != "" {
			do = do.Where(m.NeedMaintenance.Like("%" + in.Where.NeedMaintenance + "%")) // 预防性维护
		}
		if in.Where.AssetTypeName != "" {
			if in.Where.IsMc == 1 {
				do = do.Where(m.AssetTypeName.Eq(in.Where.AssetTypeName)) // 资产类型名称
			} else {
				do = do.Where(m.AssetTypeName.Like("%" + in.Where.AssetTypeName + "%")) // 资产类型名称
			}
		}

		// 处理数字类型字段，进行精确查询
		if in.Where.ParentID > 0 {
			do = do.Where(m.ParentID.Eq(in.Where.ParentID)) // 父级编码
		}
		// 处理数字类型字段，进行精确查询
		if in.Where.AssetTypeID > 0 {
			do = do.Where(m.AssetTypeID.Eq(in.Where.AssetTypeID)) // 父级编码
		}
		if in.Where.DepreciationMonths > 0 {
			do = do.Where(m.DepreciationMonths.Eq(in.Where.DepreciationMonths)) // 折旧年限
		}
		if in.Where.ManufacturerID > 0 {
			do = do.Where(m.ManufacturerID.Eq(in.Where.ManufacturerID)) // 生产厂商ID
		}
		if in.Where.FinancialCategoryID > 0 {
			do = do.Where(m.FinancialCategoryID.Eq(in.Where.FinancialCategoryID)) // 财务分类id
		}
		if in.Where.BrandID > 0 {
			do = do.Where(m.BrandID.Eq(in.Where.BrandID)) // 品牌id
		}
		if in.Where.Status > 0 {
			do = do.Where(m.Status.Eq(in.Where.Status)) // 状态
		}
		if in.Where.StockID > 0 {
			do = do.Where(m.StockID.Eq(in.Where.StockID)) // 仓库ID
		}
	}

	// 定义一个变量用于存储总记录数
	var count int64

	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.AssetCategory{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.AssetCategoryPageRes{
		Count: count,
		List:  list.([]*property_service.AssetCategory),
	}, nil
}
